package lib_model.Product;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Tax_product implements ISql {

	public int id;
	public float value;
	public int type;
	public String date_create;

	public Tax_product() {
	}

	public Tax_product(int id) {
		super();
		this.id = id;
	}

	@Override
	public ReturnObj Select() {
		String sql = "select id, value, type, date_create from dbo.tax_product";
		ReturnObj obj = new ReturnObj();
		ArrayList<Tax_product> arr = new ArrayList<Tax_product>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Tax_product a = new Tax_product();
				a.id = rs.getInt("id");
				a.value = rs.getFloat("value");
				a.type = rs.getInt("type");
				a.date_create = rs.getString("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.tax_product (value,   type ,date_create) values   (?,   ?,GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setDouble(1, this.value);
			preState.setInt(2, this.type);
			obj.Result = (preState.executeUpdate() == 1) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.tax_product set value = ?, type = ? where  id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setDouble(1, this.value);
			preState.setInt(2, this.type);
			preState.setInt(3, this.id);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.tax_product where id = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select id, value, type from dbo.tax_product where id = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setInt(1, this.id);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Tax_product a = new Tax_product();
				a.id = rs.getInt("id");
				a.value = rs.getFloat("value");
				a.type = rs.getInt("type");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
